<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>用户文章列表页</title>
    <meta name="keywords" content="" />
    <meta name="description" content="">
    <link href="css/bulma.min.css" rel="stylesheet">
    <link href="css/github-markdown.min.css" rel="stylesheet">
    <link rel="stylesheet" href="css/app.css">
    <script src="js/jquery.min.js"></script>
    <script src="js/config.js"></script>
    <script src="js/urlutil.js"></script>
    <script src="js/loginutil.js"></script>

</head>

<body>
    <nav id="navbar" class="navbar has-shadow">
        <div class="container">
            <div class="navbar-brand">
                <div class="navbar-burger burger" data-target="navMenu">
                    <span></span>
                    <span></span>
                    <span></span>
                </div>
            </div>
            <div id="navMenu" class="navbar-menu">
                <div class="navbar-start">
                    <a class="navbar-item" href="index.html">主页</a>
                    <a class="navbar-item" href="about.html">关于</a>
                </div>
                <div class="navbar-end">
                    <div id="login_div" style="display: none;" class="navbar-item">
                        <a id="category_mangager" style="display: none;background-color: #315790;"
                            class="button is-small is-primary" href="category.html">分类管理</a>&nbsp;&nbsp;&nbsp;
                        <a class="button is-small is-primary" style="background-color: #315790;"
                            href="add-article.html">写文章</a>&nbsp;&nbsp;&nbsp;&nbsp;
                        <div class="aaa">
                        <a id="login_username" href="user-articlelist.html" style="color: #373737"></a>&nbsp;&nbsp;&nbsp;&nbsp;
                        <a href="javascript:logout();" style="color: #373737">退出</a>
                        </div>
                    </div>

                    <div id="unlogin_div" class="navbar-item">
                        <a class="button is-small is-primary" href="register.html">注册</a>&nbsp;&nbsp;&nbsp;&nbsp;
                        <a class="button is-small" href="login.html">登录</a>
                    </div>
                </div>
            </div>
        </div>
    </nav>

    <script>
        jQuery(function () {
            var login_username = window.localStorage.getItem("username");
            if (login_username == null) {
                // 未登录
                jQuery("#unlogin_div").show();
            } else {
                // 已经登录
                jQuery("#unlogin_div").hide();
                jQuery("#login_username").html(login_username);
                jQuery("#login_username").attr("href", "user-articlelist.html?uid=" + window.localStorage.getItem("uid"));
                jQuery("#login_div").show();
                // 管理员权限判定
                if (window.localStorage.getItem("manager") == 1) {
                    jQuery("#category_mangager").show();    // 让文章管理按钮显示
                }
            }
        });
    </script>

    <section class="hero is-primary is-bold"
        style="background-image: linear-gradient(15deg, #13547a 0%, #80d0c7 100%);">
        <div class="hero-body">
            <div class="container">
                <a href="#" onclick="location.reload(); return false;">
                    <h1 class="title" id="username" style="font-size: 2.5rem">

                    </h1>
                    <h2 class="subtitle" id="email" style="font-size: 1.5rem">

                    </h2>
                </a>

            </div>
        </div>
    </section>
    <div class="container">
        <div class="columns">
            <div class="column is-8 is-offset-2" id="articleList">

            </div>
        </div>
    </div>
    <style>
        .card {
            border-radius: 4px;
            border: 1px solid #ebeef5;
            background-color: #fff;
            overflow: hidden;
            color: #303133;
            transition: .25s;
            margin: 10px 0;
            box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);
            padding: 20px;
        }

        .card:hover {
            transform: scale(1.1);
        }

        .aaa:hover {
            font-weight: bold;
        }
    </style>
    <script>

        var uid = getUrlParam("uid");
        if (uid == null || uid <= 0) {
            lolocation.href = "404.html";
        }

        var loginUid = 0; // 当前登录用户id

        // 初始化用户信息
        function initUserinfo() {
            jQuery.ajax({
                url: req_base_url + "user/getByUid",
                method: "GET",
                headers: {
                    "Authorization": window.localStorage.getItem(token_key)
                },
                data: {
                    "uid": uid
                },
                success: function (res) {
                    // 4. 将执行的结果展示到前端
                    if (res != null && res.code == 200 && res.data != null && res.data.uid > 0) {
                        // 成功
                        jQuery("#username").html(res.data.username);
                        jQuery("#email").html(res.data.email);
                        loginUid = res.data.loginUid;
                    } else {
                        // 失败
                        location.href = "error.html";
                        alert("初始化失败 " + res.msg);
                    }

                }
            })
        }


        // 文章删除
        function doArtDel(aid) {
            if (confirm("是否确认删除?")) {
                jQuery.ajax({
                    url: req_base_url + "article/delByAid",
                    method: "POST",
                    headers: {
                        "Authorization": window.localStorage.getItem(token_key)
                    },
                    data: {
                        "aid": aid
                    },
                    success: function (res) {
                        if (res != null && res.code == 200) {
                            // 成功
                            alert("删除成功!");
                            location.href = location.href;
                        } else {
                            // 失败
                            location.href = "error.html";
                            alert("删除失败 " + res.msg);
                        }

                    }
                })
            }

        }



        // 初始化用户的文章列表信息
        function initUserArticleList() {
            jQuery.ajax({
                url: req_base_url + "article/getUserArticle",
                method: "GET",
                data: {
                    "uid": uid
                },
                success: function (res) {
                    // 4. 将执行的结果展示到前端
                    if (res != null && res.code == 200 && res.data != null && res.data.length >= 0) {
                        // 成功
                        var artHtml = "";
                        var isOwner = (uid == loginUid);
                        for (var i = 0; i < res.data.length; i++) {
                            var art = res.data[i];
                            artHtml += '<div class="card margin-top">';
                            artHtml += '<div class="card-content">';
                            artHtml += '<div class="content">';
                            artHtml += '<a href="detail.html?aid=' + art.aid + '" style="color: #3b3b3b;font-weight: bold;">' + art.title + '</a>';
                            artHtml += '</div>';
                            artHtml += '</div>';
                            if (isOwner == true) {
                                artHtml += '<footer class="card-footer">';
                                artHtml += '<a href="edit-article.html?aid=' + art.aid + '" class="card-footer-item">修改</a>';
                                artHtml += '<a href="javascript:doArtDel(' + art.aid + ');" class="card-footer-item">删除</a>';
                                artHtml += '</footer>';
                            }
                            artHtml += '</div>';
                        }
                        jQuery("#articleList").html(artHtml);
                    } else {
                        // 失败
                        location.href = "error.html";
                        alert("初始化失败 " + res.msg);
                    }

                }
            })
        }


        // 初始化当前页面信息
        function initPage() {
            if (uid == null || uid <= 0) {
                location.href("404.html");
                return false;
            }

            // 1. 初始化用户信息
            initUserinfo();

            // 2. 初始化用户的文章列表信息
            initUserArticleList();
        }
        initPage();

    </script>
</body>

</html>